﻿// Pages/Devices/Index.cshtml.cs

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using NetworkFlowMonitor.Scripts.Data;

namespace NetworkFlowMonitor.Pages.Devices;

public partial class IndexModel : PageModel
{
    private readonly ApplicationDbContext _context;

    public IndexModel(ApplicationDbContext context)
    {
        _context = context;
    }

    public List<Scripts.Service.Device> Devices { get; set; } = new();

    public async Task OnGetAsync()
    {
        Devices = await _context.Devices.ToListAsync();
    }

    public async Task<IActionResult> OnPostUpdateAsync(int id, string deviceName, string? remark, int speedLimit,
        bool isBlocked)
    {
        var device = await _context.Devices.FindAsync(id);
        if (device != null)
        {
            device.DeviceName = deviceName;
            device.Remark = remark;
            device.SpeedLimitKbps = speedLimit;
            device.IsBlocked = isBlocked;

            Console.WriteLine($"更新设备: {device.MacAddress}  {remark}  {deviceName}  {isBlocked}");
            await _context.SaveChangesAsync();
        }

        return RedirectToPage();
    }
}